package test;

import com.alibaba.fastjson.JSON;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.suxin.config.DatePropertyEditor;
import com.suxin.domain.User;
import com.suxin.enums.UserStatus;
import org.junit.Test;
import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.interceptor.CustomizableTraceInterceptor;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.MutablePropertyValues;
import org.springframework.context.expression.MethodBasedEvaluationContext;
import org.springframework.core.DefaultParameterNameDiscoverer;
import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.WebDataBinder;

import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.*;

/**
 * @author Tang
 * @classname NoContextLoad
 * @description [  ]
 * @date 2021/12/5 11:21
 */
public class NoContextLoad {

    public static void main(String[] args) {
        ExpressionParser parser = new SpelExpressionParser();
        Expression expression = parser.parseExpression("'hello' + ' world!'");
        System.out.println(expression.getValue().toString());
    }

    @Test
    public void test01() throws Exception{
        ExpressionParser parser = new SpelExpressionParser();
//        User user = new User("tang",12, UserStatus.ONLINE);
        User user = new User();
        EvaluationContext context = new StandardEvaluationContext();
        context.setVariable("user",user);
        context.setVariable("name","tang");
        System.out.println(parser.parseExpression("#user.name").getValue(context).toString());
//        System.out.println(parser.parseExpression("{deveryUser{#user.name}}").getValue(context).toString());

        Method method = user.getClass().getDeclaredMethod("run", String.class);
        MethodBasedEvaluationContext evaluationContext = new MethodBasedEvaluationContext(user,method,new Object[]{"100"},
                new DefaultParameterNameDiscoverer());

        System.out.println(parser.parseExpression("#speed").getValue(evaluationContext).toString());
    }


    @Test
    public void test03(){
        HashMap<Object, String> newHashMap = Maps.newHashMap();
        newHashMap.put("name", "tang");
        String prefix_1 = "#{${";
        String prefix_2 = "}}";
        PropertyPlaceholderHelper helper = new PropertyPlaceholderHelper(prefix_1,prefix_2);
        String s1 = helper.replacePlaceholders("#{${name}}", (key) -> {
            System.out.println(key);
            return newHashMap.get(key);
        });
        System.out.println(s1);
    }


    @Test
    public void test02(){
        ProxyFactory proxyFactory = new ProxyFactory(new User());
        proxyFactory.setProxyTargetClass(true);

        CustomizableTraceInterceptor traceInterceptor = new CustomizableTraceInterceptor();

        Advisor advisor = new DefaultPointcutAdvisor(traceInterceptor);
        proxyFactory.addAdvisor(advisor);

        User user = (User)proxyFactory.getProxy();
        user.run("100");
    }

    @Test
    public void test04(){
        User user = new User();
        WebDataBinder binder = new WebDataBinder(user, "person");

        // 设置属性（此处演示一下默认值）
        MutablePropertyValues pvs = new MutablePropertyValues();

        // 使用!来模拟各个字段手动指定默认值
        //pvs.add("name", "fsx");
        pvs.add("!name", "不知火舞");
        pvs.add("age", 18);
        pvs.add("!age", 10); // 上面有确切的值了，默认值不会再生效

        binder.bind(pvs);
        System.out.println(user);
    }

    @Test
    public void test05(){
        User user = new User();
        ServletRequestDataBinder binder = new ServletRequestDataBinder(user, "user");

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addParameter("name", "tang");
        request.addParameter("age", "18");
        request.addParameter("date", "2022-09-12");
        request.addParameter("list", "4", "2", "3", "1");
        request.addParameter("map['key1']", "value1");
        request.addParameter("map['key2']", "value2");

        binder.bind(request);

        System.out.println(user);
    }

    @Test
    public void test06(){
        User user = new User();

        ServletRequestDataBinder binder = new ServletRequestDataBinder(user, "user");
        // org.springframework.beans.PropertyEditorRegistrySupport.createDefaultEditors
        // 自定义类型转换
        DatePropertyEditor datePropertyEditor = new DatePropertyEditor();
        binder.registerCustomEditor(Date.class,datePropertyEditor);

        MockHttpServletRequest request = new MockHttpServletRequest();
        request.addParameter("name", "tang");
        request.addParameter("age", "18");
        request.addParameter("date", "2022-09-12");
        request.addParameter("enDate", "Sat Jul 20 11:00:22 CST 2019");
        request.addParameter("list", "4", "2", "3", "1");
        request.addParameter("map['key1']", "value1");
        request.addParameter("map['key2']", "value2");

        binder.bind(request);

        System.out.println(user);
    }

    @Test
    public void test07(){
        final ArrayList<Map<String,Object>> list = Lists.newArrayList();
        System.out.println(JSON.toJSONString(list));
    }

}